package cc.liyinghao.controller;


import cc.liyinghao.common.api.Result;
import cc.liyinghao.response.MessageUserListVo;
import cc.liyinghao.response.MessageVo;
import cc.liyinghao.param.RecordParam;
import cc.liyinghao.param.SendMessageParam;
import cc.liyinghao.security.util.SecurityUtil;
import cc.liyinghao.service.ChatListService;
import cc.liyinghao.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天信息路由
 */
@RestController
@Slf4j
@RequestMapping("/message")
@RequiredArgsConstructor
@Api(tags = "信息路由")
public class MessageController {

    private final MessageService messageService;
    private final ChatListService chatListService;

    @PostMapping("/send_message")
    @ApiOperation(value = "发送消息")
    public Result<String> sendMessage(SendMessageParam message) {
        messageService.send(SecurityUtil.getCurrentUserId(), message);
        return Result.success();
    }

    @GetMapping("/get_message_by_id")
    @ApiOperation(value = "根据用户id获取聊天记录(单聊)")
    public Result<List<MessageVo>> getMessage(RecordParam recordParam) {
        if (recordParam.getTargetId() == null || recordParam.getTargetId().isEmpty()) {
            return Result.error("目标id不能为空");
        }
        if (recordParam.getIndex() < 0 || recordParam.getNum() < 0 || recordParam.getNum() > 100) {
            return Result.error("请求异常");
        }
        return Result.success(
                messageService.record(SecurityUtil.getCurrentUserId(), recordParam)
        );
    }

    @GetMapping("/get_chat_contacts_by_id")
    @ApiOperation(value = "获取用户消息列表(chat_list)")
    public Result<List<MessageUserListVo>> getChatContactsMessage() {
        return Result.success(
                chatListService.getChatList(SecurityUtil.getCurrentUserId())
        );
    }
}
